<template>
    <div style="margin-top: 90px;margin-left: 350px">
      <el-form v-model="plan">
        <el-form-item label="招生计划时间" prop="planData" v-loading="loading">
          <span v-if="modifyPlan">{{plan.planData}}</span>
          <el-date-picker
            v-if="!modifyPlan"
            v-model="plan.planData"
            value-format="yyyy-MM-dd"
            type="date"
            placeholder="选择日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="招生计划目标" prop="planTarget">
          <el-input
            style="width: 180px"
            placeholder="请输入内容"
            v-model="plan.planTarget"
            :disabled="modifyPlan">
          </el-input>
        </el-form-item>
        <el-form-item label="招生计划详情" prop="planDetail">
          <el-input
            style="width: 380px"
            type="textarea"
            :rows="5"
            :disabled="modifyPlan"
            placeholder="请输入内容"
            v-model="plan.planDetail">
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button @click="modifyPlan = false" v-if="modifyPlan">修改</el-button>
          <el-button @click="updatePlan" v-if="!modifyPlan" type="primary">确定</el-button>
        </el-form-item>
      </el-form>
    </div>
</template>

<script>
import { initPlan, addPlan, updatePlan } from '../../../api/stu/plan'

export default {
  data() {
    return {
      loading: false,
      modifyPlan: true,
      plan: {
        planId: 0,
        planData: '',
        planTarget: '',
        planDetail: ''
      }
    }
  },
  methods: {
    // 修改计划
    updatePlan() {
      this.loading = true
      if (this.plan.planId != 0){
        // 修改
        updatePlan(this.plan).then(response => {
          console.log(response)
          this.init()
          this.modifyPlan = true
          this.$message({
            message: response.msg,
            type: 'success'
          })
        })
      } else {
        // 添加
        addPlan(this.plan).then(response => {
          console.log(response)
          this.init()
          this.modifyPlan = true
          this.$message({
            message: response.msg,
            type: 'success'
          })
        })
      }
      this.loading = false
    },
    // 初始化计划
    init(){
      initPlan().then(response => {
        console.log(response)
        if (response.data !== null){
          this.plan = response.data
        }else {
          this.modifyPlan = false
        }
      })
    }
  },
  created() {
    this.init()
  }
}
</script>

<style scoped>

</style>
